package com.cjd.common.recycler

import android.view.ViewPropertyAnimator
import androidx.core.view.ViewCompat
import androidx.recyclerview.widget.RecyclerView


/**
 * @Author chenjidong
 * @email 374122600@qq.com
 * created 2019/9/24
 * description recycler item 缩放动画
 */
class ScaleItemAnimator : BaseItemAnimator() {

    override fun setRemoveAnimation(
        holder: RecyclerView.ViewHolder,
        animator: ViewPropertyAnimator
    ) {
        ViewCompat.setPivotX(holder.itemView, (holder.itemView.width / 2).toFloat())
        ViewCompat.setPivotY(holder.itemView, (holder.itemView.height / 2).toFloat())
        animator.scaleX(0f).scaleY(0f)
    }

    override fun removeAnimationEnd(holder: RecyclerView.ViewHolder) {
        ViewCompat.setScaleX(holder.itemView, 1f)
        ViewCompat.setScaleY(holder.itemView, 1f)
    }

    override fun addAnimationInit(holder: RecyclerView.ViewHolder) {
        ViewCompat.setScaleX(holder.itemView, 0f)
        ViewCompat.setScaleY(holder.itemView, 0f)
    }

    override fun setAddAnimation(holder: RecyclerView.ViewHolder, animator: ViewPropertyAnimator) {
        ViewCompat.setPivotX(holder.itemView, (holder.itemView.width / 2).toFloat())
        ViewCompat.setPivotY(holder.itemView, (holder.itemView.height / 2).toFloat())
        animator.scaleX(1f).scaleY(1f)
    }

    override fun addAnimationCancel(holder: RecyclerView.ViewHolder) {
        ViewCompat.setScaleX(holder.itemView, 1f)
        ViewCompat.setScaleY(holder.itemView, 1f)
    }

    override fun setOldChangeAnimation(
        holder: RecyclerView.ViewHolder,
        animator: ViewPropertyAnimator
    ) {
        ViewCompat.setPivotX(holder.itemView, (holder.itemView.width / 2).toFloat())
        ViewCompat.setPivotY(holder.itemView, (holder.itemView.height / 2).toFloat())
        animator.scaleX(0f).scaleY(0f)
    }

    override fun oldChangeAnimationEnd(holder: RecyclerView.ViewHolder) {
        ViewCompat.setScaleX(holder.itemView, 1f)
        ViewCompat.setScaleY(holder.itemView, 1f)
    }

    override fun newChangeAnimationInit(holder: RecyclerView.ViewHolder) {
        ViewCompat.setScaleX(holder.itemView, 0f)
        ViewCompat.setScaleY(holder.itemView, 0f)
    }

    override fun setNewChangeAnimation(
        holder: RecyclerView.ViewHolder,
        animator: ViewPropertyAnimator
    ) {
        ViewCompat.setPivotX(holder.itemView, (holder.itemView.width / 2).toFloat())
        ViewCompat.setPivotY(holder.itemView, (holder.itemView.height / 2).toFloat())
        animator.scaleX(1f).scaleY(1f)
    }

    override fun newChangeAnimationEnd(holder: RecyclerView.ViewHolder) {
        ViewCompat.setScaleX(holder.itemView, 1f)
        ViewCompat.setScaleY(holder.itemView, 1f)
    }
}